package com.eagle.rate.dao;

// default package

import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.eagle.rate.entity.Rate;

/**
 * A data access object (DAO) providing persistence and search support for Rate
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see .Rate
 * @author MyEclipse Persistence Tools
 */
@Repository
public class RateDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(RateDAO.class);
	// property constants
	public static final String TID = "tid";
	public static final String OID = "oid";
	public static final String USER_ID = "userId";
	public static final String NICK = "nick";
	public static final String RESULT = "result";
	public static final String ITEM_TITLE = "itemTitle";
	public static final String ITEM_PRICE = "itemPrice";
	public static final String CONTENT = "content";
	public static final String SELLER_NAME = "sellerName";
	public static final String SELLER_MOBILE = "sellerMobile";

	protected void initDao() {
		// do nothing
	}

	public void save(Rate transientInstance) {
		log.debug("saving Rate instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Rate persistentInstance) {
		log.debug("deleting Rate instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Rate findById(java.lang.Integer id) {
		log.debug("getting Rate instance with id: " + id);
		try {
			Rate instance = (Rate) getHibernateTemplate().get("Rate", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Rate> findByExample(Rate instance) {
		log.debug("finding Rate instance by example");
		try {
			List<Rate> results = (List<Rate>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Rate instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Rate as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Rate> findByTid(Object tid) {
		return findByProperty(TID, tid);
	}

	public List<Rate> findByOid(Object oid) {
		return findByProperty(OID, oid);
	}

	public List<Rate> findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List<Rate> findByNick(Object nick) {
		return findByProperty(NICK, nick);
	}

	public List<Rate> findByResult(Object result) {
		return findByProperty(RESULT, result);
	}

	public List<Rate> findByItemTitle(Object itemTitle) {
		return findByProperty(ITEM_TITLE, itemTitle);
	}

	public List<Rate> findByItemPrice(Object itemPrice) {
		return findByProperty(ITEM_PRICE, itemPrice);
	}

	public List<Rate> findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List<Rate> findBySellerName(Object sellerName) {
		return findByProperty(SELLER_NAME, sellerName);
	}

	public List<Rate> findBySellerMobile(Object sellerMobile) {
		return findByProperty(SELLER_MOBILE, sellerMobile);
	}

	public List findAll() {
		log.debug("finding all Rate instances");
		try {
			String queryString = "from Rate";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Rate merge(Rate detachedInstance) {
		log.debug("merging Rate instance");
		try {
			Rate result = (Rate) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Rate instance) {
		log.debug("attaching dirty Rate instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Rate instance) {
		log.debug("attaching clean Rate instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static RateDAO getFromApplicationContext(ApplicationContext ctx) {
		return (RateDAO) ctx.getBean("RateDAO");
	}
}